package net.flyinggroup.vinsguru;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;

import java.time.Duration;


@Slf4j
public class FluxCreator {
    @SneakyThrows
    public static void main(String[] args) {

        Flux<Integer> integerFlux = Flux.create((FluxSink<Integer> fluxSink) -> {
            Flux.interval(Duration.ofMillis(100))
                    .log()
                    .subscribe(l -> {
                        log.info("emit:{}", l);
                        fluxSink.next(Math.toIntExact(l));
                    });
        });
        //First observer. takes 1 ms to process each element
        integerFlux.delayElements(Duration.ofMillis(1)).subscribe(i -> System.out.println("First :: " + i));

        //Second observer. takes 2 ms to process each element
        integerFlux.delayElements(Duration.ofMillis(2)).subscribe(i -> System.out.println("Second:: " + i));
        Thread.sleep(3000);


    }
}
